//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.artofsolving.jodconverter.office;

import com.sun.star.beans.PropertyValue;
import com.sun.star.uno.UnoRuntime;
import java.io.File;
import java.util.Iterator;
import java.util.Map;
import org.artofsolving.jodconverter.util.PlatformUtils;

public class OfficeUtils {
    public static final String SERVICE_DESKTOP = "com.sun.star.frame.Desktop";

    private OfficeUtils() {
        throw new AssertionError("utility class must not be instantiated");
    }

    public static <T> T cast(Class<T> type, Object object) {
        return UnoRuntime.queryInterface(type, object);
    }

    public static PropertyValue property(String name, Object value) {
        PropertyValue propertyValue = new PropertyValue();
        propertyValue.Name = name;
        propertyValue.Value = value;
        return propertyValue;
    }

    public static PropertyValue[] toUnoProperties(Map<String, ?> properties) {
        PropertyValue[] propertyValues = new PropertyValue[properties.size()];
        int i = 0;

        Map.Entry entry;
        Object value;
        for(Iterator i$ = properties.entrySet().iterator(); i$.hasNext(); propertyValues[i++] = property((String)entry.getKey(), value)) {
            entry = (Map.Entry)i$.next();
            value = entry.getValue();
            if (value instanceof Map) {
                Map<String, Object> subProperties = (Map)value;
                value = toUnoProperties(subProperties);
            }
        }

        return propertyValues;
    }

    public static String toUrl(File file) {
        String path = file.toURI().getRawPath();
        String url = path.startsWith("//") ? "file:" + path : "file://" + path;
        return url.endsWith("/") ? url.substring(0, url.length() - 1) : url;
    }

    public static File getDefaultOfficeHome() {
        if (System.getProperty("office.home") != null) {
            return new File(System.getProperty("office.home"));
        } else if (PlatformUtils.isWindows()) {
            String programFiles = System.getenv("ProgramFiles(x86)");
            if (programFiles == null) {
                programFiles = System.getenv("ProgramFiles");
            }

            return findOfficeHome(programFiles + File.separator + "OpenOffice.org 3", programFiles + File.separator + "LibreOffice 3");
        } else {
            return PlatformUtils.isMac() ? findOfficeHome("/Applications/OpenOffice.org.app/Contents", "/Applications/LibreOffice.app/Contents") : findOfficeHome("/opt/openoffice.org3", "/opt/libreoffice", "/usr/lib/openoffice", "/usr/lib/libreoffice");
        }
    }

    private static File findOfficeHome(String... knownPaths) {
        String[] arr$ = knownPaths;
        int len$ = knownPaths.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            String path = arr$[i$];
            File home = new File(path);
            if (getOfficeExecutable(home).isFile()) {
                return home;
            }
        }

        return null;
    }

    public static File getOfficeExecutable(File officeHome) {
        return PlatformUtils.isMac() ?
                new File(officeHome, "MacOS/soffice")
                : new File(officeHome, "program/soffice.bin");
    }
}
